package com.dim.list_orders;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class items_manager extends Activity {
	private SQLiteDatabase database;

	private Spinner s_items_type;
	private EditText et_name;

	private Cursor cursor_it_t;
	private int id_it_t;
	private long id_it;
	private String name_it;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(com.dim.list_orders.R.layout.items_manager);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				com.dim.list_orders.R.layout.title);
		TextView title = (TextView) findViewById(R.id.title);
		title.setText(com.dim.list_orders.R.string.l_items_manager);

		Bundle extras = getIntent().getExtras();
		id_it = extras.getLong("id_it");
		id_it_t = extras.getInt("id_it_t");
		name_it = extras.getString("name_it");

		s_items_type = (Spinner) findViewById(com.dim.list_orders.R.id.s_items_type);
		et_name = (EditText) findViewById(com.dim.list_orders.R.id.et_name);

		class_sqlite dbOpenHelper = new class_sqlite(this,
				getString(com.dim.list_orders.R.string.db_name));
		database = dbOpenHelper.openDataBase();

		stopManagingCursor(cursor_it_t);

		cursor_it_t = database.query("items_type",
				new String[] { "_id", "name" }, null, null, null, null, "name");

		startManagingCursor(cursor_it_t);

		SimpleCursorAdapter Adapt_users = new SimpleCursorAdapter(this,
				android.R.layout.simple_spinner_item, cursor_it_t,
				new String[] { "name" }, new int[] { android.R.id.text1 });
		Adapt_users
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s_items_type.setAdapter(Adapt_users);

		edit();
	}

	private void edit() {

		if (id_it > 0) {
			et_name.setText(name_it);
			for (int u = 0; u < s_items_type.getCount(); u++) {
				Cursor cu = (Cursor) s_items_type.getItemAtPosition(u);
				startManagingCursor(cu);
				if (cu.getLong(cu.getColumnIndex("_id")) == id_it_t) {
					s_items_type.setSelection(u);
					break;
				}
			}

		}
	}

	public void b_add(View v) {
		ContentValues initialValues = new ContentValues();

		int id_u = cursor_it_t.getInt(cursor_it_t.getColumnIndex("_id"));

		initialValues.put("id_it_t", id_u);
		initialValues.put("name", et_name.getText().toString());

		if (id_it > 0) {
			String[] args = { String.valueOf(id_it) };
			database.update("items", initialValues, "_id=?", args);
		} else
			database.insert("items", null, initialValues);
		onBackPressed();
	}

	protected void onDestroy() {
		super.onDestroy();
		database.close();
	}
}
